package com.clkj.xiuzheng.controller;

import com.clkj.xiuzheng.pojo.Customer;
import com.clkj.xiuzheng.pojo.CustomerVisit;
import com.clkj.xiuzheng.pojo.OutStock;
import com.clkj.xiuzheng.pojo.User;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.service.CustomerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/17 15:50:57
 **/
@RestController
@RequestMapping(value = "/customer")
@Api(value = "/customer", tags = {"客户管理"})
@CrossOrigin
public class CustomerController {
    @Autowired
    private CustomerService customerService;;

    @ApiOperation(value = "添加、修改客户信息")
    @PostMapping("/addCustomer")
    public Result addCustomer( Customer customer, HttpServletRequest request){
        try {
            customerService.addCustomer(customer,request);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation(value = "获取业务员的客户列表")
    @PostMapping("/lisUserCustomer")
    public Result lisUserCustomer(@ApiParam(name = "userId", value = "业务员Id") @RequestParam("userId") Integer userId){
        try {
            List<Customer> customerList = customerService.lisUserCustomer(userId);
            return new Result(true, StatusCode.OK,"查询成功",customerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "添加业务员客户拜访")
    @PostMapping("/addVisit")
    public Result addVisit(CustomerVisit visit){
        try {
             customerService.addVisit(visit);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "获取客户拜访详情")
    @PostMapping("/listCustomerVisit")
    public Result listCustomerVisit(Integer customerId){
        try {
            List<CustomerVisit> visitList = customerService.listCustomerVisit(customerId);
            return new Result(true, StatusCode.OK,"查询成功",visitList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "获取业务员近7天拜访记录")
    @PostMapping("/listCustomerVisitBySeven")
    public Result listCustomerVisitBySeven(Integer userId){
        try {
            List<CustomerVisit> visitList = customerService.listCustomerVisitBySeven(userId);
            return new Result(true, StatusCode.OK,"查询成功",visitList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据业务员名称查询业务员")
    @PostMapping("/getVisitUserByName")
    public Result getVisitUserByName(@RequestParam Integer pageSize,
                                     @RequestParam Integer pageNum,
                                     @RequestParam String  userName){
        try {
            PageInfo pageInfo = customerService.getVisitUserByName(pageSize,pageNum,userName);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "业务员获取验证码")
    @PostMapping("/userLogin")
    public Result userLogin(@RequestParam String userTel ){
        try {
            Map map  = customerService.userLogin(userTel);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "业务员获取验证码")
    @PostMapping("/login")
    public Result login(@RequestParam String userTel ){
        try {
            User user  = customerService.login(userTel);
            return new Result(true, StatusCode.OK,"查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


}
